import { computed } from "vue";
import { useAuthStore } from "@/stores/modules/auth";

/**
 * @description 页面按钮权限
 */
export const useAuthButtons = () => {
  // 获取用户的权限按钮列表
  const authStore = useAuthStore();

  // 将权限按钮数组转换为对象，便于快速查找
  const authButtonsObj = computed(() => {
    return authStore.authButtonListGet.reduce(
      (prev, cur) => {
        prev[cur] = true;
        return prev;
      },
      {} as Record<string, boolean>
    );
  });

  /**
   * @description 判断按钮权限
   * @param btn 按钮权限标识
   * @returns 是否有权限
   */
  const BUTTONS = (btn: string): boolean => {
    // 如果当前权限包含 "*:*:*"，则拥有所有按钮权限
    return authButtonsObj.value["*:*:*"] || !!authButtonsObj.value[btn];
  };

  return {
    BUTTONS
  };
};
